<template>
  <div class="container">
    <div class="container_body">
      <div v-for="(img, index) in errors" :key="img.id" class="error_code_container">
        <div class="error_code_img">
          <el-image lazy :src="img.codeUrl" :preview-src-list="[img.codeUrl]"></el-image>
        </div>
        <div class="error_code_info">
          <el-form inline size="mini">
            <el-form-item :label="'准考证号:'">
              <el-input v-model="img.examCode"></el-input>
            </el-form-item>
            <el-form-item label-width="0">
              <el-button @click="handlePreview(img.targetUrl)" type="text">查看原图</el-button>
              <el-button type="text" @click="() => {img.examCode = ''}">无名卷</el-button>
              <el-button @click="handleDelete(img)" type="text" style="color: red">删除</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>

    <div class="container_controller">
      <div class="left_margin"></div>
      <div class="buttons">
        <el-button-group>
          <el-button @click="handleErrorItems">修改</el-button>
        </el-button-group>
      </div>
      <div class="pager">
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page.current"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="page.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total">
        </el-pagination>
      </div>
    </div>
    <el-dialog :visible.sync="preview">
      <el-image lazy :src="previewSrc" :preview-src-list="[previewSrc]"></el-image>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "uploadPhotos",
  props: ['workId', "currentCode"],
  data() {
    // const errors = []
    // for (let i = 0; i < 10; i++) {
    //   errors.push({
    //     src: "https://picsum.photos/300/200",
    //     examCode: "123456",
    //     source: "https://picsum.photos/300/200",
    //     sourceImg: "https://picsum.photos/1028/720",
    //
    //   })
    // }
    return {
      errors: [],
      preview: false,
      previewSrc: '',
      page: {
        current: 1,
        size: 10,
        total: 0
      },
    }
  },
  created() {
    this.handleQueryErrorPage()

  },
  mounted() {
  },
  methods: {
    handleDelete(row) {
      this.$confirm('确认删除？')
          .then(_ => {
            this.deleteItem(row)
          })
          .catch(_ => {
          });

    },
    async deleteItem(row) {
      const res = await this.$api.deleteSheet({
        uploadId: row.uploadId
      })
      if (res.code !== 200) {
        this.$message.warning(res.message)
        return
      }
      this.handleQueryErrorPage()
    },
    async handleErrorItems() {
      const loading = this.$fastLoading()
      try {
        let params = {
          "workId": this.workId,
          "editUploads": this.errors.map(item => {
            return {
              uploadId: item.uploadId,
              examCode: item.examCode
            }
          })
        }
        let res = await this.$api.errorCodeHandler(params)
        if (res.code !== 200) {
          this.$message.warning("处理失败")
        } else {
          this.handleQueryErrorPage()
        }
      } finally {
        loading.close()
      }

    },
    async handleQueryErrorPage() {
      const loading = this.$fastLoading()
      try {
        let res = await this.$api.errorCodePage(
            {
              current: this.page.current,
              size: this.page.size,
              data: {
                workId: this.workId,
                errorCode: this.currentCode
              }
            }
        )

        if (res.code !== 200) {
          this.$message.warning("请求失败");
        }
        this.errors = res.data.data
        this.page.total = res.data.total
      } finally {
        loading.close()
      }
    },
    handlePreview(src) {
      this.previewSrc = src
      this.preview = true
    },
    handleSizeChange(size) {
      this.page.size = size
      this.handleQueryErrorPage()
    },
    handleCurrentChange(current) {
      this.page.current = current
      this.handleQueryErrorPage()
    },
  }
}
</script>

<style scoped type="less">
.container {
  box-sizing: border-box;
  height: 100%;
  width: 100%;
  overflow: auto;

  //justify-content: space-around;

  .container_body {
    display: flex;
    flex-wrap: wrap;

    .error_code_container {
      padding: 1px;
      width: 300px;

      .error_code_img {
        height: 200px;

      }

      .error_code_info {
        margin-top: 10px;

      }

      .error_code_controller {

      }
    }
  }

  .container_controller {
    display: flex;
    .left_margin {
      flex: 1;
    }
    .buttons {
      margin-right: 30px;

    }
  }

}
</style>
